#include "contacts.pb.h"
#include <iostream>
#include <fstream>

void addPersonInfo(contacts2::PeopleInfo *info)
{
    std::cout << "----------添加新联系人----------" << std::endl;
    // 输入姓名
    std::cout << "请输入姓名: ";
    std::string name;
    std::getline(std::cin, name);
    info->set_name(name);

    // 输入年龄
    std::cout << "请输入年龄: ";
    int age;
    std::cin >> age;
    info->set_age(age);

    std::cin.ignore(256, '\n');

    // 输入电话
    while (1)
    {
        // 1.设置电话号码
        std::cout << "请输入电话号码(回车结束): ";
        std::string phone_number;
        std::getline(std::cin, phone_number);
        if (phone_number.empty())
            break;

        contacts2::PeopleInfo_Phone *phone = info->add_phone();
        phone->set_number(phone_number);

        // 2.设置电话类型
        std::cout << "请选择电话类型(0.移动 1.固定): ";
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 0:
            phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MOB);
            break;
        case 1:
            phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            std::cout << "非法类型，使用默认值" << std::endl;
            break;
        }
    }

    // 输入地址
    std::cout << "请输入家庭地址: ";
    std::string home_addr;
    std::getline(std::cin, home_addr);

    std::cout << "请输入单位地址: ";
    std::string unit_addr;
    std::getline(std::cin, unit_addr);

    contacts2::Address addr;
    addr.set_home_addr(home_addr);
    addr.set_unit_addr(unit_addr);

    info->mutable_data()->PackFrom(addr);

    // 选择其他联系方式
    std::cout << "选择一种其它联系方式 (1.QQ 2.微信): ";
    int input = 0;
    std::cin >> input;
    std::cin.ignore(256, '\n');

    std::string target_number;
    std::string wechat;
    switch (input)
    {
    case 1:
        std::cout << "请输入QQ号: ";
        std::getline(std::cin, target_number);
        info->set_qq(target_number);
        break;
    case 2:
        std::cout << "请输入微信号: ";
        std::getline(std::cin, target_number);
        info->set_wechat(target_number);
        break;
    default:
        std::cout << "非法请求！" <<std::endl;
        abort();
        break;
    }

    // 添加备注
    while(1)
    {
        std::string comment_title;
        std::string comment_content;
        std::cout << "请输入备注标题(回车结束): ";
        std::getline(std::cin, comment_title);
        if(comment_title.empty())
            break;

        std::cout << "请输入备注内容: ";
        std::getline(std::cin, comment_content);

        info->mutable_comments()->insert({comment_title, comment_content});
    }

    std::cout << "----------添加联系人成功----------" << std::endl;
}

// 将通讯录的信息写入文件中
int main()
{
    // 0.先读取文件中已有的数据
    std::ifstream ifs("contacts.bin", std::ios::in | std::ios::binary);
    contacts2::Contacts contacts;
    if (ifs) // 检查文件是否存在，若不存在，则无需读取
        contacts.ParseFromIstream(&ifs);
    ifs.close();

    // 1.添加新的通讯人信息
    addPersonInfo(contacts.add_info());

    // 2.通讯人信息写到文件中
    std::ofstream ofs("contacts.bin", std::ios::out | std::ios::binary | std::ios::trunc);
    if (!ofs)
    {
        std::cerr << "Failed to open file" << std::endl;
        ofs.close();
        return -1;
    }
    if (!contacts.SerializeToOstream(&ofs))
    {
        std::cerr << "Failed to write` to ostream" << std::endl;
        ofs.close();
        return -2;
    }

    ofs.close();
    return 0;
}